<?php
/**
 * Name: SmsLogic.class.php
 * Description: 短信模块
 * User: DIY
 * Date: 2016/9/23
 * Time: 18:15
 *
 *
 *
 * 发送DEMO
//模拟短信发送
$Sms = new SmsLogic();
$param['username'] = '吴志忠';
$param['date'] = date('Y-m-d H:i:s',time());
$param['address'] = '宫宵国际二楼KTV';
$param['money'] = '10000元';
$r = $Sms->sendSms('15840609981','test','PC'.time(),$param);
var_dump($r);
 */

namespace Common\Logic;


use Common\Model\SmsLogModel;
use Common\Model\SmsTemplateModel;

class SmsLogic{

    private $SmsTemplate;
    private $SmsLog;

    /*
     * 构造函数
     * */
    public function __construct(){

        $this->SmsTemplate = new SmsTemplateModel();
        $this->SmsLog = new SmsLogModel();
    }

    /*
     * 发送短信
     * @param phone 手机号码
     * @param type 短信模版类型
     * @param data = array('key'=>value) //data中需要替换的字段和值
     * @auto 默认是即时 1：即时 0：调度
     * */
    public function sendSms($phone,$type,$nid,$data,$auto=1){
        //验证手机号码
        if(!check_phone($phone)){
            return array('result'=>'error','msg'=>'手机号码格式错误');
        }
        //验证nid短信是否重复发送
        $check_nid = $this->SmsLog
            ->where(array('nid'=>$nid))
            ->find();
        if(!empty($check_nid)){
            return array('result'=>'error','msg'=>'该短信已经发送');
        }
        //取得短信模版
        $content = $this->getSmsContent($type,$data);
        if($content['result']!='success'){
            return $content;
        }
        //插入短信记录
        $param = array(
            'type'=>$type,
            'nid'=>$nid,
            'is_auto'=>$auto,
            'phone'=>$phone,
            'content'=>$content['content'],
            'add_time'=>time(),
            'status'=>'0',
        );
        $this->SmsLog->add($param);
        $send_result = $this->send($phone,$content);
        //处理状态
        $status = $send_result['status']==1?1:2;
        //更新到数据库
        $param = array(
            'nid'=>$nid,
            'status'=>$status,
            'send_time'=>time(),
            'result'=>$send_result['msg'],
        );
        $this->SmsLog->where(array('nid'=>$nid))->save($param);
        if($send_result['status']==1){ //短信发送成功
            return array('result'=>'success','msg'=>$send_result['msg']);
        }
        else{
            return array('result'=>'error','msg'=>$send_result['msg']);
        }
    }


    /*
     * 重发短信 针对失败短信重新发送
     * @param id 短信ID
     * */
    public function reSendSms($id){
        $sms = $this->SmsLog->where(array('status'=>'2'))->find($id);
        if(empty($sms)){
            return array('result'=>'error','msg'=>'短信记录不存在');
        }
        $send_result = $this->send($sms['phone'],$sms['content']);
        //处理状态
        $status = $send_result['status']==1?1:2;
        $param = array(
            'id'=>$id,
            'status'=>$status,
            'send_time'=>time(),
            'result'=>$send_result['msg'],
        );
        $this->SmsLog->data($param)->save();
        if($send_result['status']==1){ //短信发送成功
            return array('result'=>'success','msg'=>$send_result['msg']);
        }
        else{
            return array('result'=>'error','msg'=>$send_result['msg']);
        }
    }

    /*
     * 获取短信内容
     * @param type 短信模版标识
     * @param data = array('key'=>value) //data中需要替换的字段和值
     * return Sting
     * */
    private function getSmsContent($type,$data){

        //验证类别
        if($type==''){
            return array('result'=>'error','mgs'=>'短信模版标识不能为空');
        }

        $template = $this->SmsTemplate
            ->where(array('type'=>$type))
            ->find();
        if(empty($template)){
            return array('result'=>'error','mgs'=>'短信模版不存在');
        }
        $content = $template['content'];
        foreach($data as $k => $v){
            $content = str_replace('{'.$k.'}',$v,$content);
        }
        return array('result'=>'success','content'=>$content);
    }

    /*
     * 发送短信核心
     * 根据不同短信平台发送短信。
     * @param phone
     * @param content
     * return array('status','msg) 返回 1成功 0失败 msg消息
     * */
    private function send($phone,$content){
        //关闭短信发送 默认发送成功
        if(C('SMS_STATUS')){
            return array('status'=>1,'msg'=>'发送成功');
        }
        return array('status'=>1,'msg'=>'发送成功');
    }

}